package cn.geapi.core.util.security.impl;

import org.apache.log4j.Logger;

import cn.geapi.core.util.security.IHashIdStrategy;
import fm.jiecao.lib.Hashids;

public class ArticleHashIdStrategyImpl implements IHashIdStrategy {
	private final static Logger LOGGER = Logger
			.getLogger(ArticleHashIdStrategyImpl.class);
	private static final String ARTICLE_SALT = "768a67ebe1c0a350feb91e9ca0bae50f";
	private static final int MINI_HASH_LEN = 8;
	private static Hashids hashids;
	private static Hashids hashidsNew;
	static {
		try {
			hashids = new Hashids(ARTICLE_SALT);
			hashidsNew = new Hashids(ARTICLE_SALT, MINI_HASH_LEN);
		} catch (Exception e) {
			LOGGER.error("create hashid instance faild!" + e.getMessage());
			e.printStackTrace();
		}
	}

	public String encrypt(Long id) {
		return hashidsNew.encrypt(id);
	}

	public Long decrypt(String encrytId) {
		if (encrytId.length() < 8) {
			return hashids.decrypt(encrytId)[0];
		} else {
			return hashidsNew.decrypt(encrytId)[0];
		}

	}

}
